import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
import AppLayout from '../views/AppLayout.vue' // 导入布局组件

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'home',
        component: HomeView,
        meta: { public: true } // 公共页面，无需登录
    },
    {
        path: '/login',
        name: 'login',
        component: LoginView,
        meta: { public: true } // 公共页面，无需登录
    },

    {
        path: '/user', // 创建一个父路由用于布局
        component: AppLayout,
        children: [
            {
                path: 'house-search',
                name: 'HouseSearch',
                component: () => import('../views/User/HouseSearch.vue')
            },
            {
                path: 'first-index',
                name: 'FirstIndex',
                component: ()=>  import('../views/FirstIndex.vue'),
            },
            {
                path: 'quarter-analysis',
                name: 'QuarterAnalysis',
                component: () => import('../views/User/QuarterAnalysis.vue')
            },
            {
                path: 'AreaRanking',
                name: 'AreaRanking',
                component: () => import('../views/User/AreaRankingChart.vue')
            },
            {
                path: 'PriceRanking',
                name: 'PriceRanking',
                component: () => import('../views/PriceRanking/PriceRanking.vue')
            },
            {
                path: 'price/ranking1',
                name: 'ranking1',
                component: () => import('../views/PriceRanking/HousePriceRanking.vue')
            },
            {
                path: 'price/ranking2',
                name: 'ranking2',
                component: () => import('../views/PriceRanking/AreaPriceRanking.vue')
            },
            {
                path: 'price/ranking3',
                name: 'ranking3',
                component: () => import('../views/PriceRanking/MonthPriceRanking.vue')
            },
            {
                path: 'user-management',
                name: 'UserManagement',
                component: () => import('../views/Admin/UserManagement.vue'),
                meta: { requiresAdmin: true } // 需要管理员权限
            },
            {
                path: 'MyProfile', // 新增的个人中心路由
                name: 'MyProfile',
                component: () => import('../views/User/MyProfile.vue')
            }

        ]
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

// 修复导航重复错误
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => {
        if (err.name !== 'NavigationDuplicated') {
            throw err
        }
        return err
    })
}

const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location) {
    return originalReplace.call(this, location).catch(err => {
        if (err.name !== 'NavigationDuplicated') {
            throw err
        }
        return err
    })
}

// ❗ 路由守卫：控制访问权限
router.beforeEach((to, from, next) => {
    const user = JSON.parse(localStorage.getItem('user'))

    // 判断目标路由是否是公共页面
    const isPublicPage = to.matched.some(record => record.meta.public)

    // 如果不是公共页面且没有登录，则跳转到登录页
    if (!isPublicPage && !user) {
        console.log('未登录，跳转到登录页')
        return next({ name: 'login' })
    }

    // 如果是需要管理员权限的页面
    if (to.meta.requiresAdmin) {
        if (user && user.isLeader === 1) {
            return next()
        } else {
            console.log('无管理员权限，重定向到首页')
            return next({ name: 'home' })
        }
    }

    // 正常放行
    next()
})

export default router
